import axios from 'axios'
import { mockApi } from '../mock'

// 创建 axios 实例
const instance = axios.create({
  baseURL: process.env.VUE_APP_API_BASE_URL || '',
  timeout: 15000
})

// 请求拦截器
instance.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  error => Promise.reject(error)
)

// 响应拦截器
instance.interceptors.response.use(
  response => response.data,
  error => Promise.reject(error)
)

export const api = {
  auth: {
    login: (credentials) => {
      // 使用模拟数据
      return mockApi.login(credentials.username, credentials.password)
    }
  },
  user: {
    getInfo: () => {
      // 使用模拟数据
      return mockApi.getUserInfo()
    }
  },
  distributor: {
    getStats: () => {
      return axios.get('/api/distributor/stats')
        .then(response => response.data)
    }
  }
}

export default api 